/**
 * Filename: DBHelper.java
 *
 * @author Firefly
 * @contact postfirefly@163.com
 * @time 2016/3/17 16:08
 */
package com.github.postsanf.yinian.db;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

/**
 * 忆年维护和管理数据库的基类
 */
public class DBHelper extends SQLiteOpenHelper {
    // 数据库名
    private static final String DATABASE_NAME = "yinian.db";
    // 版本号
    private static final int DATABASE_VERSION = 12;

    // CursorFactory置为null,使用默认值
    public DBHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    // 版本变更调用
    public DBHelper(Context context, int version) {
        super(context, DATABASE_NAME, null, version);
    }

    // 第一次创建时调用
    @Override
    public void onCreate(SQLiteDatabase db) {
        // 状态列表
        String ynUpprogress = "CREATE TABLE IF NOT EXISTS upprogress" +
                "(id INTEGER   PRIMARY KEY  AUTOINCREMENT," +                // AUTOINCREMENT_ID
                " upuid       varchar(50) NOT NULL," +                       // UserID
                " upgid       text," +                       // GroupID
                " upgimid       text," +                       // Group环信ID
                " upisofficial  INTEGER," +                                  // upisofficial
                " upgname     varchar(25)," +                                // GroupName
                " uptype      INTEGER," +                                    // GroupType
                " uppush      INTEGER," +                                    // uppush
                " upcontent   text," +                                       // Content
                " upeplace   text," +                                       // Eplace
                " uptime      varchar(25)," +                                // Time
                " upsize      varchar(25)," +                                // Size
                " upstatus    INTEGER," +                                // Status
                " upfirstimg  varchar(255)," +                             // FirstImg
                " upismemorycard   INTEGER," +                                  // upismemorycard
                " uptags    text," +                                // tags
                " upaudio    text," +                                // audio
                " upstorage    varchar(25)," +                                // storage
                " upmemorytime    text," +                                // memorytime
                " upplace    text," +                                // place
                " upcardstyle    text," +                                 // cardstyle
                " upaudiotime    text," +                               // upaudiotime
                " upcoverurl    text," +                                  // upcoverurl
                " upplacepic    text," +                               // upplacepic
                " upplacelongitude    text," +                               // upplacelongitude
                " upplacelatitude    text," +                               // upplacelatitude
                " uppeoplename    text," +                               // uppeoplename
                " upmain    text," +                               // upmain
                " upsource    text," +                               // upsource
                " upispush    text," +                               // upispush
                " verifypicaddress    text," +                               // verifypicaddress
                " totalnum    text," +                               // totalnum
                " totalmoney    text);";                               // totalmoney
        db.execSQL(ynUpprogress);
        // 图片列表
        String ynUpimgkey = "CREATE TABLE IF NOT EXISTS upimgkey" +
                "(id INTEGER PRIMARY KEY AUTOINCREMENT," +                 // ID
                " upid      INTEGER," +                                    // pID
                " upaddress varchar(255)," +                               // Address
                " upimgsize varchar(25)," +                                // upimgsize
                " upkey     varchar(50));";                               // key
        db.execSQL(ynUpimgkey);
    }

    // 每次打开数据库之后首先被执行
    @Override
    public void onOpen(SQLiteDatabase db) {
        super.onOpen(db);
    }

    // 版本更新数据库时调用
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        exits(db, "upprogress");
        exits(db, "upimgkey");
        String ynUpprogress = "CREATE TABLE IF NOT EXISTS upprogress" +
                "(id INTEGER   PRIMARY KEY  AUTOINCREMENT," +                // AUTOINCREMENT_ID
                " upuid       varchar(50) NOT NULL," +                       // UserID
                " upgid       text," +                       // GroupID
                " upgimid       text," +                       // Group环信ID
                " upisofficial  INTEGER," +                                  // upisofficial
                " upgname     varchar(25)," +                                // GroupName
                " uptype      INTEGER," +                                    // GroupType
                " uppush      INTEGER," +                                    // uppush
                " upcontent   text," +                                       // Content
                " upeplace   text," +                                       // Eplace
                " uptime      varchar(25)," +                                // Time
                " upsize      varchar(25)," +                                // Size
                " upstatus    INTEGER," +                                // Status
                " upfirstimg  varchar(255)," +                             // FirstImg
                " upismemorycard   INTEGER," +                                  // upismemorycard
                " uptags    text," +                                // tags
                " upaudio    text," +                                // audio
                " upstorage    varchar(25)," +                                // storage
                " upmemorytime    text," +                                // memorytime
                " upplace    text," +                                // place
                " upcardstyle    text," +                                 // cardstyle
                " upaudiotime    text," +                               // upaudiotime
                " upcoverurl    text," +                                  // upcoverurl
                " upplacepic    text," +                               // upplacepic
                " upplacelongitude    text," +                               // upplacelongitude
                " upplacelatitude    text," +                               // upplacelatitude
                " uppeoplename    text," +                               // uppeoplename
                " upmain    text," +                               // upmain
                " upsource    text," +                               // upsource
                " upispush    text," +                               // upispush
                " verifypicaddress    text," +                               // verifypicaddress
                " totalnum    text," +                               // totalnum
                " totalmoney    text);";                               // totalmoney
        db.execSQL(ynUpprogress);
        // 图片列表
        String ynUpimgkey = "CREATE TABLE IF NOT EXISTS upimgkey" +
                "(id INTEGER PRIMARY KEY AUTOINCREMENT," +                 // ID
                " upid      INTEGER," +                                    // pID
                " upaddress varchar(255)," +                               // Address
                " upimgsize varchar(25)," +                                // upimgsize
                " upkey     varchar(50));";                               // key
        db.execSQL(ynUpimgkey);
    }

    public boolean exits(SQLiteDatabase db, String table) {
        boolean exits = false;
        String sql = "select * from sqlite_master where name=" + "'" + table + "'";
        Cursor cursor = db.rawQuery(sql, null);
        if (cursor.getCount() != 0) {
            exits = true;
            db.execSQL("DROP TABLE " + table);
        }
        return exits;
    }
}